package com.example.ormsqlsplicingframe1.proxy;

import java.util.HashMap;
import java.util.Map;

public class LazyProxyFactory implements IProxyFactory{

    private SyncProxyFactory syncProxyFactory;

    private static ThreadLocal<Map<Object,LazyProxy>>threadLocal = ThreadLocal.withInitial(() -> new HashMap<>());


    private static  Map<Object, LazyProxy> getCache(){
        Map<Object, LazyProxy> cache = threadLocal.get();
        return cache;
    }


    public LazyProxyFactory(SyncProxyFactory syncProxyFactory) {
        this.syncProxyFactory = syncProxyFactory;
    }


    //获取代理对象
    @Override
    public Object getProxy(Object data) {
        return getCache().computeIfAbsent(data,key -> new LazyProxy(data)).getProxyInstance();
    }
}
